package j2024.j202410;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class j1031 {
    public ArrayList<Integer> findOrder (int numProject, ArrayList<ArrayList<Integer>> groups) {
        // write code here
        ArrayList<ArrayList<Integer>> edges = new ArrayList<>();
        for (int i = 0; i < numProject; i++) {
            edges.add(new ArrayList<>());
        }
        //入度
        int[] in = new int[numProject];
        for (int i = 0; i < groups.size(); i++) {
            int a = groups.get(i).get(0),b = groups.get(i).get(1);
            in[a]++;
            edges.get(b).add(a);
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numProject; i++) {
            if(in[i]==0){
                q.add(i);
            }
        }
        //拓补排序
        ArrayList<Integer> ret = new ArrayList<>();
        while(!q.isEmpty()){
            int a = q.poll();
            ret.add(a);
            for(int b:edges.get(a)){
                if(--in[b]==0){
                    q.add(b);
                }
            }
        }
        if (ret.size() == numProject) {
            return ret;
        }else {
            return new ArrayList<>();
        }
    }

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int ret = Integer.MAX_VALUE;
        for (int i = 1; i <= 26; i++) {
            int tmp = 0;
            for (int j = 0; j < str.length(); j++) {
                int s = str.charAt(j)-'a';
                tmp+=Math.min(Math.abs(i-s),26-Math.abs(i-s));
            }
            ret = Math.min(ret,tmp);
        }
        System.out.println(ret);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int[] f = new int[n+1];
        int[] g = new int[n+1];
        for (int i = 1; i <= n; i++) {
            f[i] = 1;
            for (int j = 1; j < i; j++) {
                if(arr[i]>arr[j]){
                    f[i] = Math.max(f[i],f[j]+1);
                }
            }
        }
        for (int i = n; i >= 1; i--) {
            g[i] = 1;
            for (int j = n; j > i ; j--) {
                if(arr[i]>arr[j]){
                    g[i] = Math.max(g[i],g[j]+1);
                }
            }
        }
        int len = 0;
        for (int i = 1; i <= n; i++) {
            len = Math.max(len,f[i]+g[i]-1);
        }
        System.out.println(n-len);
    }

    /**
     * 112. 路径总和
     * 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。
     * 判断该树中是否存在 根节点到叶子节点 的路径，这条路径上所有节点值相加等于目标和 targetSum 。如果存在，返回 true ；否则，返回 false 。
     *
     * 叶子节点 是指没有子节点的节点。
     * @param root
     * @param targetSum
     * @return
     */
//    public boolean hasPathSum(TreeNode root, int targetSum) {
//        if(root==null){
//            return false;
//        }
//        if(root.left==null && root.right==null){
//            return targetSum==root.val;
//        }
//        return hasPathSum(root.left,targetSum-root.val) || hasPathSum(root.right,targetSum-root.val);
//    }
}
